package com.service.impl;

import com.dao.*;
import com.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.*;
import com.util.ExampleBuilder;
import com.util.PageData;
import com.util.PageWrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @ClassName GaiqianshenqingServiceImpl
 * @Description 改签申请模块业务实现层
 */
@Service
public class GaiqianshenqingServiceImpl implements GaiqianshenqingService {

    @Autowired
    private GaiqianshenqingMapper gaiqianshenqingMapper;

    @Autowired
    private UsersMapper usersMapper;


    //保存或更新改签申请表gaiqianshenqing
    @Override
    public Integer saveOrUpdate(Gaiqianshenqing instance) {
        if(null != instance.getId() && instance.getId() > 0) {
            gaiqianshenqingMapper.updateByPrimaryKeySelective(instance);
        }else {
            gaiqianshenqingMapper.insertSelective(instance);
        }
        return instance.getId();
    }

    //查询改签申请表gaiqianshenqing
    @Override
    public List<Gaiqianshenqing> findAll() {
        List<Gaiqianshenqing> list = gaiqianshenqingMapper.selectByExample(null);
        for (Gaiqianshenqing info:list) {
            setFatherInfo(info);
        }
        return list;
    }

    //根据条件(字符类型模糊匹配)查询改签申请表gaiqianshenqing
    @Override
    public List<Gaiqianshenqing> find(Gaiqianshenqing instance) {
        GaiqianshenqingExample gaiqianshenqingExample = getGaiqianshenqingExampleByLike(instance);
        gaiqianshenqingExample.setOrderByClause(" id desc");
        List<Gaiqianshenqing> list = gaiqianshenqingMapper.selectByExample(gaiqianshenqingExample);
        for (Gaiqianshenqing info:list) {
            setFatherInfo(info);
        }
        return list;
     }

    //根据条件(字符类型完全匹配)查询改签申请表gaiqianshenqing
    @Override
    public List<Gaiqianshenqing> findByEqualTo(Gaiqianshenqing instance) {
        GaiqianshenqingExample gaiqianshenqingExample = getGaiqianshenqingExampleByEqualTo(instance);
        gaiqianshenqingExample.setOrderByClause(" id desc");
        List<Gaiqianshenqing> list = gaiqianshenqingMapper.selectByExample(gaiqianshenqingExample);
        for (Gaiqianshenqing info:list) {
            setFatherInfo(info);
        }
        return list;
    }


    //根据条件(字符类型模糊匹配)查询改签申请表gaiqianshenqing最后一条数据
    @Override
    public Gaiqianshenqing findOne(Gaiqianshenqing instance) {
        List<Gaiqianshenqing> list = find(instance);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //根据条件(字符类型完全匹配)查询改签申请表gaiqianshenqing最后一条数据
    @Override
    public Gaiqianshenqing findOneByEqualTo(Gaiqianshenqing instance) {
        List<Gaiqianshenqing> list = findByEqualTo(instance);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    //根据id列查询改签申请表gaiqianshenqing数据
    @Override
    public Gaiqianshenqing findById(Integer id) {
        Gaiqianshenqing info = gaiqianshenqingMapper.selectByPrimaryKey(id);
        setFatherInfo(info);
        return info;
    }

    //根据主键id列删除改签申请表gaiqianshenqing数据
    @Override
    public int deleteById(Integer id) {
        return gaiqianshenqingMapper.deleteByPrimaryKey(id);
    }


    //根据条件(字符类型模糊匹配)分页查询改签申请表gaiqianshenqing数据
    @Override
    public PageData<Gaiqianshenqing> findPage(PageWrap<Gaiqianshenqing> pageWrap) {
        PageHelper.startPage(pageWrap.getPageNum(), pageWrap.getPageSize());
        GaiqianshenqingExample gaiqianshenqingExample = getGaiqianshenqingExampleByLike(pageWrap.getModel());
        gaiqianshenqingExample.setOrderByClause(pageWrap.getOrderByClause());
        List<Gaiqianshenqing> list = gaiqianshenqingMapper.selectByExample(gaiqianshenqingExample);
        for (Gaiqianshenqing info:list) {
            setFatherInfo(info);
        }
        return PageData.from(new PageInfo<>(list));
    }

    //根据条件(字符类型完全匹配)分页查询改签申请表gaiqianshenqing数据
    @Override
    public PageData<Gaiqianshenqing> findPageByEqualTo(PageWrap<Gaiqianshenqing> pageWrap) {
        PageHelper.startPage(pageWrap.getPageNum(), pageWrap.getPageSize());
        GaiqianshenqingExample gaiqianshenqingExample = getGaiqianshenqingExampleByEqualTo(pageWrap.getModel());
        gaiqianshenqingExample.setOrderByClause(pageWrap.getOrderByClause());
        List<Gaiqianshenqing> list = gaiqianshenqingMapper.selectByExample(gaiqianshenqingExample);
        for (Gaiqianshenqing info:list) {
            setFatherInfo(info);
        }
        return PageData.from(new PageInfo<>(list));
    }

    //父表信息
    private void setFatherInfo(Gaiqianshenqing info){
        if (null != info){
            Users users= usersMapper.selectByPrimaryKey(info.getUsersid());
            info.setUsers(users);

        }
    }

    //字符类型模糊查询条件构造器
    private GaiqianshenqingExample getGaiqianshenqingExampleByLike(Gaiqianshenqing instance) {
        GaiqianshenqingExample gaiqianshenqingExample = new GaiqianshenqingExample();
        GaiqianshenqingExample.Criteria criteria = gaiqianshenqingExample.createCriteria();
        //改签申请id完全匹配
        if (null != instance.getId()) {
            criteria.andIdEqualTo(instance.getId());
        }
        //姓名xingming模糊匹配
        if (null != instance.getXingming() && !instance.getXingming().equals("")) {
            criteria.andXingmingLike("%" + instance.getXingming() + "%");
        }
        //电话dianhua模糊匹配
        if (null != instance.getDianhua() && !instance.getDianhua().equals("")) {
            criteria.andDianhuaLike("%" + instance.getDianhua() + "%");
        }
        //身份证号shenfenzhenghao模糊匹配
        if (null != instance.getShenfenzhenghao() && !instance.getShenfenzhenghao().equals("")) {
            criteria.andShenfenzhenghaoLike("%" + instance.getShenfenzhenghao() + "%");
        }
        //原始班次banci模糊匹配
        if (null != instance.getBanci() && !instance.getBanci().equals("")) {
            criteria.andBanciLike("%" + instance.getBanci() + "%");
        }
        //改签班次gaiqiancheci模糊匹配
        if (null != instance.getGaiqiancheci() && !instance.getGaiqiancheci().equals("")) {
            criteria.andGaiqiancheciLike("%" + instance.getGaiqiancheci() + "%");
        }
        //备注说明beizhu模糊匹配
        if (null != instance.getBeizhu() && !instance.getBeizhu().equals("")) {
            criteria.andBeizhuLike("%" + instance.getBeizhu() + "%");
        }
        //时间shijian模糊匹配
        if (null != instance.getShijian() && !instance.getShijian().equals("")) {
            criteria.andShijianLike("%" + instance.getShijian() + "%");
        }
        //是否审核shenhe模糊匹配
        if (null != instance.getShenhe() && !instance.getShenhe().equals("")) {
            criteria.andShenheLike("%" + instance.getShenhe() + "%");
        }
        //用户idusersid完全匹配
        if (null != instance.getUsersid()) {
            criteria.andUsersidEqualTo(instance.getUsersid());
        }

        return gaiqianshenqingExample;
    }

    //字符类型完全相等查询条件构造器
    private GaiqianshenqingExample getGaiqianshenqingExampleByEqualTo(Gaiqianshenqing instance) {
        GaiqianshenqingExample gaiqianshenqingExample = new GaiqianshenqingExample();
        GaiqianshenqingExample.Criteria criteria = gaiqianshenqingExample.createCriteria();
        //改签申请id完全匹配
        if (null != instance.getId()) {
            criteria.andIdEqualTo(instance.getId());
        }
        //姓名xingming完全匹配
        if (null != instance.getXingming() && !instance.getXingming().equals("")) {
            criteria.andXingmingEqualTo(instance.getXingming());
        }
        //电话dianhua完全匹配
        if (null != instance.getDianhua() && !instance.getDianhua().equals("")) {
            criteria.andDianhuaEqualTo(instance.getDianhua());
        }
        //身份证号shenfenzhenghao完全匹配
        if (null != instance.getShenfenzhenghao() && !instance.getShenfenzhenghao().equals("")) {
            criteria.andShenfenzhenghaoEqualTo(instance.getShenfenzhenghao());
        }
        //原始班次banci完全匹配
        if (null != instance.getBanci() && !instance.getBanci().equals("")) {
            criteria.andBanciEqualTo(instance.getBanci());
        }
        //改签班次gaiqiancheci完全匹配
        if (null != instance.getGaiqiancheci() && !instance.getGaiqiancheci().equals("")) {
            criteria.andGaiqiancheciEqualTo(instance.getGaiqiancheci());
        }
        //备注说明beizhu完全匹配
        if (null != instance.getBeizhu() && !instance.getBeizhu().equals("")) {
            criteria.andBeizhuEqualTo(instance.getBeizhu());
        }
        //时间shijian完全匹配
        if (null != instance.getShijian() && !instance.getShijian().equals("")) {
            criteria.andShijianEqualTo(instance.getShijian());
        }
        //是否审核shenhe完全匹配
        if (null != instance.getShenhe() && !instance.getShenhe().equals("")) {
            criteria.andShenheEqualTo(instance.getShenhe());
        }
        //用户idusersid完全匹配
        if (null != instance.getUsersid()) {
            criteria.andUsersidEqualTo(instance.getUsersid());
        }

        return gaiqianshenqingExample;
    }
}
